<?php

/**
 *  @FileComment            [后台商品模型]
 *  @ProjectName             项目名称：shopFrame
 *  @FileName                   文件名称：GoodModel.class.php
 *  @Author                       yuerengui <596580041@qq.com>
 *  @Date                          2014-10-10 20:51:32
 *  *
 */
class GoodsModel extends Model {

        public $table = 'goods';
        public $validate = array(
            array('type_tid', 'nonull', '请选择所属类型', 2, 3),
            array('cid', 'nonull', '请选择所属分类', 2, 3),
            array('bid', 'nonull', '请选择所属品牌', 2, 3),
            array('gname', 'nonull', '请输入商品标题', 2, 3),
            array('lhname', 'maxlen:45', '次标题最多为45个字符', 2, 3),
            array('unit', 'nonull', '商品单位不能为空', 2, 3),
            array('mprice', 'nonull', '请填写商品的市场价', 2, 3),
            array('jdprice', 'nonull', '请填写商品的京东价', 2, 3),
            array('mprice', 'regexp:/^(([1-9]\d*)(\.\d{1,2})?)$|(0\.0?([1-9]\d?))$/', '商城价格式不正确！', 2, 3),
            array('jdprice', 'regexp:/^(([1-9]\d*)(\.\d{1,2})?)$|(0\.0?([1-9]\d?))$/', '京东价格式不正确！', 2, 3),
            array('listimg', 'nonull', '请上传列表图片', 2, 3),
            array('thumb', 'nonull', '请上传商品图片', 2, 3),
        );
        public $auto = array(
            array('listimg', '_img', 'method', 2, 3),
            array('thumb', '_serialize', 'method', 2, 3)
        );

        public function _serialize($data) {
                $data = serialize($data);
                return $data;
        }

        public function _img($data) {
                $Temp = array();
                if ($data) {
                        foreach ($data as $v) {
                                $Temp = $v['path'];
                        }
                        return $Temp;
                }
                return '';
        }

        /**
         *  [编辑商品]
         */
        public function edit_data() {
                $data = array(
                    'gname' => Q('post.gname'),
                    'hlname' => Q('post.lhname'),
                    'mprice' => Q('post.mprice', 1000, 'intval'),
                    'jdprice' => Q('post.jdprice', 1000, 'intval'),
                    'listimg' => $this->data['listimg'],
                    'addtime' => time(),
                    'stock' => Q('post.stock', 1000, 'intval'),
                    'brand_bid' => Q('post.bid', 0, 'intval'),
                    'category_cid' => Q('post.cid', 0, 'intval'),
                    'type_tid' => Q('post.type_tid', 0, 'intval'),
                    'click' => Q('post.click', 0, 'intval')
                );
                $gid = Q('post.gid', 0, 'intval');
                //插入商品表
                $this->where(array('gid' => $gid))->update($data);
                //商品属性的添加
                $goodAttr = K('GoodsAttr');
                $goodAttr->where(array('goods_gid' => $gid))->del();
                $attr_id = K('TypeAttr')->get_filed_data(Q('post.type_tid', 0, 'intval'), 'taid', 0);
                $stand_id = K('TypeAttr')->get_filed_data(Q('post.type_tid', 0, 'intval'), 'taid', 1);
                if (!$attr_id) {
                        $attr_id = array();
                }
                if (!$stand_id) {
                        $stand_id = array();
                }
                //组合前台post发送过来的属性name和规格name和附加价格
                foreach ($attr_id as $v) {
                        $attr = Q('post.attr_' . $v);
                        $tempData = array(
                            'ganame' => $attr,
                            'added' => Q('post.attr_' . $v . '_added', 0, 'intval'),
                            'type_attr_taid' => $v,
                            'goods_gid' => $gid
                        );
                        $goodAttr->add($tempData);
                }
                //商品规格的添加
                foreach ($stand_id as $v) {
                        $stand = Q('post.standard_' . $v);
                        if (!$stand) {
                                $stand = array();
                        }
                        foreach ($stand as $k => $value) {
                                $tempArr = array(
                                    'ganame' => $value,
                                    'added' => Q('post.standard_' . $v . '_' . $k . '_added', 0, 'intval'),
                                    'goods_gid' => $gid,
                                    'type_attr_taid' => $v
                                );
                                $goodAttr->add($tempArr);
                        }
                }
                //商品细节表的添加
                $Gdetail = array(
                    'thumb' => $this->_serialize($_POST['thumb']),
                    'detailCon' => $_POST['detailCon'],
                    'service' => $_POST['service'],
                    'goods_gid' => $gid
                );
                K('GoodsDetail')->where(array('goods_gid' => $gid))->update($Gdetail);
        }

        /**
         *  [添加商品]
         */
        public function add_data() {
                //商品表的添加
                $data = array(
                    'gname' => Q('post.gname'),
                    'hlname' => Q('post.lhname'),
                    'mprice' => Q('post.mprice', 1000, 'intval'),
                    'jdprice' => Q('post.jdprice', 1000, 'intval'),
                    'listimg' => $this->data['listimg'],
                    'addtime' => time(),
                    'stock' => Q('post.stock', 1000, 'intval'),
                    'brand_bid' => Q('post.bid', 0, 'intval'),
                    'category_cid' => Q('post.cid', 0, 'intval'),
                    'type_tid' => Q('post.type_tid', 0, 'intval'),
                    'click' => Q('post.click', 0, 'intval')
                );
                $gid = $this->add($data);
                $attr_id = K('TypeAttr')->get_filed_data(Q('post.type_tid', 0, 'intval'), 'taid', 0);
                $stand_id = K('TypeAttr')->get_filed_data(Q('post.type_tid', 0, 'intval'), 'taid', 1);
                //商品属性的添加
                if (!$attr_id) {
                        $attr_id = array();
                }
                if (!$stand_id) {
                        $stand_id = array();
                }
                $goodAttr = K('GoodsAttr');
                foreach ($attr_id as $v) {
                        $attr = Q('post.attr_' . $v);
                        $tempData = array(
                            'ganame' => $attr,
                            'added' => Q('post.attr_' . $v . '_added', 0, 'intval'),
                            'type_attr_taid' => $v,
                            'goods_gid' => $gid
                        );
                        $goodAttr->add($tempData);
                }
                //商品规格的添加
                foreach ($stand_id as $v) {
                        $stand = Q('post.standard_' . $v);
                        if (!$stand) {
                                $stand = array();
                        }
                        foreach ($stand as $k => $value) {
                                $tempArr = array(
                                    'ganame' => $value,
                                    'added' => Q('post.standard_' . $v . '_' . $k . '_added', 0, 'intval'),
                                    'goods_gid' => $gid,
                                    'type_attr_taid' => $v
                                );
                                $goodAttr->add($tempArr);
                        }
                }
                //商品细节表的添加
                $Gdetail = array(
                    'thumb' => $this->_serialize($_POST['thumb']),
                    'detailCon' => $_POST['detailCon'],
                    'service' => $_POST['service'],
                    'goods_gid' => $gid
                );
                K('GoodsDetail')->add($Gdetail);
        }

        public function get_gid($where) {
                return $this->where($where)->getField('gid', true);
        }

        public function get_one_data($gid) {
                return $this->where(array('gid' => $gid))->find();
        }

        /**
         *  [删除商品]
         * @param type $gid
         */
        public function del_data($gid) {
                //删除商品表
                $data = $this->where(array('gid' => $gid))->find();
                unset($data['listimg']);
                $flag = $this->where(array('gid' => $gid))->del();
                //删除商品详细表
                $goodsDetail = K('GoodsDetail');
                $data = $goodsDetail->where(array('goods_gid' => $gid))->find();
                unset($data['thumb']);
                unset($data['smallimg']);
                unset($data['bigimg']);
                if ($flag) {
                        $goodsDetail->where(array('goods_gid' => $gid))->del();
                        //删除商品属性表
                        K('GoodsAttr')->where(array('goods_gid' => $gid))->del();
                        K('GoodsList')->where(array('goods_gid' => $gid))->del();
                }
        }

        //修改商品的默认货号
        public function update_comb($gid, $data) {
                return $this->where(array('gid' => $gid))->update($data);
        }

        public function get_like_data($where = NULL, $limit = NULL, $order = NULL) {
                return $this->limit($limit)->where($where)->order($order)->all();
        }

}
